//
//  AppDelegate.swift
//  sss
//
//  Created by 顾佳槟 on 2018/3/6.
//  Copyright © 2018年 lancoo. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    //后台任务
    var backgroundTask:UIBackgroundTaskIdentifier?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        self.window = UIWindow.init(frame: UIScreen.main.bounds)
        self.window?.rootViewController = RootViewController.init(ViewController())
        self.window?.makeKeyAndVisible()
        
        return true
    }
    
    func applicationWillResignActive(_ application: UIApplication) {
        print("app即将被暂停")
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
        print("app已经进入后台")
        
        //如果已存在后台任务，先将其设为完成
        if self.backgroundTask != nil {
            application.endBackgroundTask(self.backgroundTask!)
            self.backgroundTask = UIBackgroundTaskInvalid
        }
        
        //如果要后台运行
        
        //注册后台任务
        self.backgroundTask = application.beginBackgroundTask(expirationHandler: {
            () -> Void in
            //如果没有调用endBackgroundTask，时间耗尽时应用程序将被终止
            application.endBackgroundTask(self.backgroundTask!)
            self.backgroundTask = UIBackgroundTaskInvalid
        })
    }
    
    func applicationWillEnterForeground(_ application: UIApplication) {
        print("app即将进入前台")
    }
    
    func applicationDidBecomeActive(_ application: UIApplication) {
        print("app已经被激活")
    }
    
    func applicationWillTerminate(_ application: UIApplication) {
        print("app即将被杀死")
    }
    
    
}

